<?php

namespace addons\shop\controller;

use addons\shop\model\CertificateM;

class Certificate extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function list()
    {
        $param         = $this->request->param();
        $type          = $this->request->get('type', 1);
        $sort_id       = $this->request->get('sort_id', '');
        $param['type'] = $type;
        if ($sort_id) {
            $param['sort_id'] = $sort_id;
        }
        //$train = Db::name("certificate")->where($where)->select();
        $train = \addons\shop\model\Certificate::tableList($param);
        $this->view->assign('data', $train);
        $this->view->assign('type', $type);
        $this->view->assign('param', $param);
        $this->view->assign('t_type', 7);
        return $this->view->fetch('/certificate/list');
    }

    public function search()
    {
        if ($this->request->isGet()) {
            return $this->fetch('/certificate/search', [
                't_type' => 7,
            ]);
        }
        $sn        = input('sn');
        $code      = input('code');
        $r['code'] = 0;

        $certificate = CertificateM::where([
            'code'      => $sn,
            'file_code' => $code,
        ])->find();
        if ($certificate) {
            $r['code'] = 1;
            $r['url']  = $certificate['img'];
        }

        return json($r);
    }
}
